package admin

import (
	"errors"
	"github.com/gin-gonic/gin"
	"pingbuqingyun_django/app/models/form"
	"pingbuqingyun_django/app/models/form/memberForm"
	"pingbuqingyun_django/app/services/admin"
	"pingbuqingyun_django/pkg/constant"
	"pingbuqingyun_django/pkg/tools"
)

// AddMemberController
//
//	@Description: 添加会员
//	@param c
func AddMemberController(c *gin.Context) {
	forms := memberForm.MemberAddForm{}
	err := c.ShouldBindJSON(&forms)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	member, err := admin.MemberService.CreateMember(forms)
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "创建会员成功！", member)
}

func UpdateMemberController(c *gin.Context) {
	forms := memberForm.MemberUpdateForm{}
	err := c.ShouldBindJSON(&forms)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	err = admin.MemberService.UpdateMember(forms)
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "更新会员信息成功！", nil)
}

// SearchMemberList
//
//	@Description: 搜索会员
//	@param c
func SearchMemberListController(c *gin.Context) {
	searchQuery := memberForm.MemberSearchForm{}
	err := c.ShouldBindQuery(&searchQuery)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	res, _ := c.Get(constant.PAGINATION_KEY)
	pageInfo := res.(form.PaginateForm)
	data := admin.MemberService.SearchMemberList(searchQuery, pageInfo)
	tools.JsonSuccessData(c, "搜索会员列表成功", data)
}

// DeleteMemberController
//
//	@Description: 删除会员信息
//	@param c
func DeleteMemberController(c *gin.Context) {
	deleteForm := memberForm.MemberIdForm{}
	err := c.ShouldBindJSON(&deleteForm)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	res, _ := c.Get(constant.JWT_INFO_KEY)
	err = admin.MemberService.DeleteMember(int(res.(uint)), deleteForm.Id)
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "删除会员成功", err)
}

// MemberCountController
//
//	@Description: 会员数量分类统计
//	@param c
func MemberCountController(c *gin.Context) {
	data := admin.MemberService.CountMember()
	tools.JsonSuccessData(c, "", data)
}

// DeactivateMemberController
//
//	@Description: 禁用会员接口
//	@param c
func DeactivateMemberController(c *gin.Context) {
	uForm := memberForm.DeactivateMemberForm{}
	err := c.ShouldBindJSON(&uForm)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	uId, _ := c.Get(constant.JWT_INFO_KEY)
	err = admin.MemberService.DeactivateMember(uForm.Uid, int(uId.(uint)))
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "操作成功！", nil)
}

func ActivateMemberController(c *gin.Context) {
	uForm := memberForm.DeactivateMemberForm{}
	err := c.ShouldBindJSON(&uForm)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	uId, _ := c.Get(constant.JWT_INFO_KEY)
	err = admin.MemberService.ActivateMember(uForm.Uid, int(uId.(uint)))
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "操作成功！", nil)
}

func RechargeMemberController(c *gin.Context) {
	uForm := memberForm.RechargeMemberForm{}
	err := c.ShouldBindJSON(&uForm)
	if err != nil {
		tools.HandleValidatorError(c, err)
		return
	}
	// 检查 NewBalance 是否小于 0
	if uForm.NewBalance < 0 {
		tools.JsonErrorResp(c, errors.New("操作失败，会员余额不足"))
		return
	}
	uId, _ := c.Get(constant.JWT_INFO_KEY)
	err = admin.MemberService.RechargeMember(int(uId.(uint)), uForm)
	if err != nil {
		tools.JsonErrorResp(c, err)
		return
	}
	tools.JsonSuccessData(c, "操作成功！", nil)
}
